package com.bjpowernode.controller;

import com.bjpowernode.entity.Income;
import com.bjpowernode.service.IncomeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 处理收入相关请求
 *
 * @author 刘逸晖
 */
@RequestMapping(value = "/myWeb/income")
public class IncomeController {

    /**
     * 处理收入相关业务
     */
    private IncomeService incomeService;

    /**
     * 新增收入类型
     *
     * @param income 预新增的收入类型
     * @return
     */
    @RequestMapping(value = "/insert.do")
    public ModelAndView insert(Income income) {
        this.incomeService.insertIncome(income);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "收入类型添加成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 查询所有收入类型
     *
     * @return 所有收入类型
     */
    @RequestMapping(value = "/findAll.do")
    public ModelAndView findAll() {
        List<Income> incomes = this.incomeService.selectAllIncomes();

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("list", incomes)
                .setViewName("income_show");

        return modelAndView;
    }

    /**
     * 根据 id 删除收入类型
     *
     * @param id 收入类型的 id
     * @return 模型和试图
     */
    @RequestMapping(value = "/delete.do")
    public ModelAndView delete(Integer id) {
        this.incomeService.deleteIncomeById(id);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "收入类型删除成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 跳转至收入类型更新页
     *
     * @param id 收入类型的 id
     * @return 模型和试图
     */
    @RequestMapping(value = "/findById.do")
    public ModelAndView findById(Integer id) {
        Income income = this.incomeService.selectIncomeById(id);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("income", income)
                .setViewName("income_edit");

        return modelAndView;
    }

    /**
     * 根据 id 更新收入类型
     *
     * @param income id 极其新数据
     * @return 收入类型查询页
     */
    @RequestMapping(value = "/update.do")
    public String update(Income income) {
        this.incomeService.updateIncomeById(income);

        return "forward:/myWeb/income/findAll.do";
    }

    @RequestMapping(value = "/ajax.do")
    @ResponseBody
    public List<Income> ajax() {
        return this.incomeService.selectAllIncomes();
    }

    public IncomeService getIncomeService() {
        return incomeService;
    }

    public void setIncomeService(IncomeService incomeService) {
        this.incomeService = incomeService;
    }
}
